#!/usr/bin/env python
######
# SYN flood handler 
# Shoufu Luo, Oct 22, 2012
######

import socket,sys
import thread
import SocketServer

if len(sys.argv) != 3:
	print "Usage: %s <ip> <port>" % sys.argv[0]
	sys.exit(1)

ip=sys.argv[1]
port=int(sys.argv[2])
agent=["1.1.2.5:9999", "1.1.2.6:9999", "1.1.2.7:9999"]

def sendCMD(thdname, target, port, msg):
	try:
		sock = socket.socket()
		sock.connect((target, port))
		sock.sendto(msg, (target, port))
	finally:
		sock.close()	

class tcp_server(SocketServer.BaseRequestHandler):
	def handle(self):
		self.data = self.request.recv(1024).strip()
		print "%s wrote:" % self.client_address[0],
		print self.data
		self.request.sendall("ACK-" + self.data)
		i = 0
		for ag in agent:
			host = ag.split(':')
			thread.start_new_thread(sendCMD, ("Thread-" + str(i), host[0], int(host[1]), self.data))
			i = i+1

if __name__ == "__main__":
	server = SocketServer.TCPServer((ip, port), tcp_server)
	print "We are listening on %d ..." % port
	server.serve_forever()

	print "We quit!"


		

